(function() {
  var __hasProp = {}.hasOwnProperty,
    __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };

  define(['views/base/view', 'text!templates/layout.html', 'lib/dao_factory', 'daos/utils_dao'], function() {
    'use strict';
    var DaoFactory, LayoutView, MENU_ITENS, MENU_PARENTS, UtilsDao, View, template, _ref;
    View = require('views/base/view');
    template = require('text!templates/layout.html');
    DaoFactory = require('lib/dao_factory');
    UtilsDao = DaoFactory.getDAOByName('utils_dao');
    MENU_PARENTS = {
      adm: {
        id: 'menu-adm',
        label: 'Menu'
      }
    };
    MENU_ITENS = [
      {
        label: 'Movimentos',
        roles: ['ROLE_ADMINISTRADOR'],
        href: '#movimento',
        parent: MENU_PARENTS.adm
      }, {
        label: 'Usuário',
        roles: ['ROLE_ADMINISTRADOR'],
        href: '#usuario',
        parent: MENU_PARENTS.adm
      }, {
        label: 'Sair',
        roles: ['ROLE_ADMINISTRADOR'],
        href: '#logout',
        parent: MENU_PARENTS.adm
      }
    ];
    return LayoutView = (function(_super) {
      __extends(LayoutView, _super);

      function LayoutView() {
        _ref = LayoutView.__super__.constructor.apply(this, arguments);
        return _ref;
      }

      LayoutView.prototype.template = template;

      LayoutView.prototype.container = '#app-container';

      LayoutView.prototype.initialize = function() {
        return LayoutView.__super__.initialize.apply(this, arguments);
      };

      LayoutView.prototype.afterRender = function() {
        LayoutView.__super__.afterRender.apply(this, arguments);
        if (window.userAuthorities) {
          this.updateMenu(window.userAuthorities);
        }
        this.subscribeEvent('layout:updateMenu', this.updateMenu);
        this.subscribeEvent('hide:menu', this.hideMenu);
        return this.subscribeEvent('show:menu', this.showMenu);
      };

      LayoutView.prototype.cacheDOMElements = function() {
        return _.extend(LayoutView.__super__.cacheDOMElements.apply(this, arguments), {
          $navbarContainer: $('.navbar-collapse ul.navbar-nav'),
          $navbar: $('.navbar')
        });
      };

      LayoutView.prototype._appendMenuItem = function(menu) {
        var menuHTML;
        menuHTML = "<li><a href=\"" + menu.href + "\">" + menu.label + "</a></li>";
        if (menu.parent && $("#" + menu.parent.id).length) {
          return $("#" + menu.parent.id).append(menuHTML);
        } else if (menu.parent) {
          this.DOMElements.$navbarContainer.append("<li class=\"dropdown\">\n  <a href=\"#\" class=\"dropdown-toggle\" data-toggle=\"dropdown\">" + menu.parent.label + " <span class=\"caret\"></span></a>\n  <ul class=\"dropdown-menu\" id=\"" + menu.parent.id + "\"></ul>\n</li>");
          return $("#" + menu.parent.id).append(menuHTML);
        } else {
          return this.DOMElements.$navbarContainer.append(menuHTML);
        }
      };

      LayoutView.prototype.updateMenu = function(authorities) {
        var a, authority, menu, _i, _len, _results;
        this.DOMElements.$navbarContainer.empty();
        if (!(authorities && _.isArray(authorities) && authorities.length)) {
          return;
        }
        _results = [];
        for (_i = 0, _len = authorities.length; _i < _len; _i++) {
          a = authorities[_i];
          authority = a.authority;
          _results.push((function() {
            var _j, _len1, _results1;
            _results1 = [];
            for (_j = 0, _len1 = MENU_ITENS.length; _j < _len1; _j++) {
              menu = MENU_ITENS[_j];
              if (_.indexOf(menu.roles, a.authority) >= 0) {
                _results1.push(this._appendMenuItem(menu));
              } else {
                _results1.push(void 0);
              }
            }
            return _results1;
          }).call(this));
        }
        return _results;
      };

      LayoutView.prototype.hideMenu = function() {
        return this.DOMElements.$navbar.hide();
      };

      LayoutView.prototype.showMenu = function() {
        return this.DOMElements.$navbar.show();
      };

      return LayoutView;

    })(View);
  });

}).call(this);
